const fs = require('fs')

const route = (handle, pathname, res, req)=>{
  console.log('About to route a request for '+pathname)

  /**
   * 判断url是否存在
   * 存在handle()
   * 不存在返回404
   */
  if(typeof handle[pathname] === 'function'){
    handle[pathname](res, req)
  }else{
    console.log(`No request handler for ${pathname}`)

    // 读取404页面
    fs.readFile('./view/404.html', (err, data)=>{
      // if(err) return console.log(err);
      res.writeHead('404', {'Content-Type': 'text-plain'})
      res.write(data)
      res.end()
    })
  }
}

exports.route = route